package com.fuzongle.tenant.config;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.BlockAttackInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;

import com.fuzongle.tenant.core.cache.EntityTableCache;
import com.fuzongle.tenant.core.mybatis.interceptor.SqlLogInterceptor;
import com.fuzongle.tenant.utils.ClassScanUtils;
import com.fuzongle.tenant.utils.StringUtils;
import org.mybatis.spring.annotation.MapperScan;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import java.util.Set;


/**
 * @auther: fuzongle
 * @date: 2021-03-24
 * @description: MybatisPlus 相关配置
 */
@Configuration
@EnableTransactionManagement
@MapperScan("com.fuzongle.tenant.business.mapper")
public class MybatisPlusConfig {
    /**
     * 单页分页条数限制(默认无限制,参见 插件#handlerLimit 方法)
     */

    static final String DEFAULT_RESOURCE_PATTERN = "**/*.class";

    /**
     * 分页配置及其乐观锁
     */
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
      /*  boolean enableTenant = tenantProperties.getEnable();
        if (enableTenant) {
            interceptor.addInnerInterceptor(tenantLineInnerInterceptor);
        }*/
        // 如果用了分页插件注意先 add TenantLineInnerInterceptor 再 add PaginationInnerInterceptor
        // 用了分页插件必须设置 MybatisConfiguration#useDeprecatedExecutor = false
//        interceptor.addInnerInterceptor(new PaginationInnerInterceptor());
        //添加数据权限插件and自定义的数据权限处理器
        //interceptor.addInnerInterceptor(new DataPermissionInterceptor(new DataPermissionHandlerImpl()));
        //分页配置
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        //乐观锁
        interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
        //防止全表更新与删除插件: BlockAttackInnerInterceptor
        interceptor.addInnerInterceptor(new BlockAttackInnerInterceptor());

        return interceptor;
    }
    @Bean
    public PaginationInnerInterceptor mapperScannerConfigurer() {
        return new PaginationInnerInterceptor();
    }

    /**
     * 配置基础扫描包
     */
    @Bean
    public MapperScannerConfigurer mapperScannerConfigurer(Environment env) {
        MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
        //设置扫描数据层
        mapperScannerConfigurer.setBasePackage(env.getProperty("mybatis-plus.base-package"));
        return mapperScannerConfigurer;
    }


    @Bean
    public SqlLogInterceptor tenantInterceptor(){
        //存储所有表实体对饮关系
        Set<String> scan = ClassScanUtils.scan("com.fuzongle.tenant.business.entity", null);
        scan.forEach(x-> {
            String substring = x.substring(x.lastIndexOf('.') + 1);
            EntityTableCache.getInstance().addCacheData(StringUtils.underscoreName(substring),x);
        });
        return new SqlLogInterceptor();
    }

}
